//
//  Track.m
//  NXTRemoteController
//
//  Created by naceka on 14.05.12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "Track.h"


@implementation Track


- (id)initWithFrame:(CGRect)frame andRobot:(Robot *) r{
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		robot = r;
		
		//background image
		UIColor * background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"view2.png"]];
		self.backgroundColor = background;
		[background release];
		
		//add Buttons
		startStop = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[startStop addTarget:self action:@selector(startStopPressed:) forControlEvents:UIControlEventTouchUpInside];
		[startStop setTitle:@"Старт" forState:UIControlStateNormal];
		startStop.frame = CGRectMake(256, 600, 256, 45);
		[self addSubview:startStop];
		
		//add timers
		//labels
		[self createLabelX:10 Y:20 W:150 H:30 andText:@"Время действий"];
		[self createLabelX:160 Y:20 W:60 H:30 andText:@"Вперед"];
		timeForwardTF = [self createUITextFieldX:230 Y:20 W:70 H:30 AndText:@"2.5"];
		[self createLabelX:310 Y:20 W:60 H:30 andText:@"Влево"];
		timeLeftTF = [self createUITextFieldX:370 Y:20 W:70 H:30 AndText:@"2.5"];
		[self createLabelX:450 Y:20 W:60 H:30 andText:@"Вправо"];
		timeRightTF = [self createUITextFieldX:520 Y:20 W:70 H:30 AndText:@"2.5"];
		[self createLabelX:600 Y:20 W:80 H:30 andText:@"Разворот"];
		timeTurnTF = [self createUITextFieldX:680 Y:20 W:70 H:30 AndText:@"2.5"];
		
		
		//load speed
		//old data
		NSUserDefaults * user_defaults = [NSUserDefaults standardUserDefaults];
		NSString * t = [user_defaults objectForKey:@"timeForward"];
		if ([t length]) {
			timeForwardTF.text = t;
			timeLeftTF.text = [user_defaults objectForKey:@"timeLeft"];
			timeRightTF.text = [user_defaults objectForKey:@"timeRight"];
			timeTurnTF.text = [user_defaults objectForKey:@"timeTurn"];
		}	
		
		//add GestureView
		//768 568
		gestureView = [[GestureView alloc] initWithFrame:CGRectMake(0, 50, 768, 518)];
		gestureView.backgroundColor = [UIColor clearColor];
		[self addSubview:gestureView];
		
		
		
		//manager
		trmanager = [[TrackManager alloc] initWithRobot:r andView:gestureView];
		[trmanager setDelegate:self];
		
		//state
		started = false;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
	NSLog(@"deallocTrack");
    [super dealloc];
}	

- (IBAction) startStopPressed:(id) sender {
	NSLog(@"startstoppressed");
	if (!started) {
		
		float forward_val = 0.0, left_val = 0.0, right_val = 0.0, turn_val = 0.0;
		
		//save time data
		NSLog(@"scan1");
		NSScanner * scaner = [NSScanner scannerWithString:timeForwardTF.text];
		[scaner scanFloat:&forward_val];
		NSLog(@"scan2");
		scaner = [NSScanner scannerWithString:timeLeftTF.text];
		[scaner scanFloat:&left_val];
		NSLog(@"scan3");
		scaner = [NSScanner scannerWithString:timeRightTF.text];
		[scaner scanFloat:&right_val];
		NSLog(@"scan4");
		scaner = [NSScanner scannerWithString:timeTurnTF.text];
		[scaner scanFloat:&turn_val];
		NSLog(@"scan5");
		NSLog(@"%f %f %f %f\n", forward_val, left_val, right_val, turn_val);
		//set up time
		[trmanager setTimeToForward:forward_val Left:left_val Right:right_val Turn:turn_val];
		
		//save data
		NSUserDefaults * user_defaults = [NSUserDefaults standardUserDefaults];
		
		//set
		[user_defaults setObject:timeForwardTF.text forKey:@"timeForward"];
		[user_defaults setObject:timeLeftTF.text forKey:@"timeLeft"];
		[user_defaults setObject:timeRightTF.text forKey:@"timeRight"];
		[user_defaults setObject:timeTurnTF.text forKey:@"timeTurn"];
		[user_defaults synchronize];
		NSLog(@"startRobotMotion");
		//run
		[trmanager startRobotMotion];
		started = true;
		[startStop setTitle:@"Стоп" forState:UIControlStateNormal];		
	}
	else {
		[trmanager stopRobotMotion];
		
		//robot stopped => change button
		[self robotStopped];
	}

	
}

- (void) robotStopped {
	started = false;
	[startStop setTitle:@"Старт" forState:UIControlStateNormal];
}

//UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	//for nxt id text editor
	[textField resignFirstResponder];
	return NO;
}

- (UITextField *) createUITextFieldX:(float) x Y:(float) y W:(float) w H:(float) h AndText:(NSString *) s {
	UITextField * res = [[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
	res.borderStyle = UITextBorderStyleRoundedRect;
	res.font = [UIFont systemFontOfSize:15];
	res.text = s;
	[s retain];
	res.autocorrectionType = UITextAutocorrectionTypeNo;
	res.keyboardType = UIKeyboardTypeDefault;
	res.returnKeyType = UIReturnKeyDone;
	res.clearButtonMode = UITextFieldViewModeWhileEditing;
	res.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;   
	[res setKeyboardType:UIKeyboardTypeNumberPad];
	res.delegate = self;
	[self addSubview:res];
	return res;
}

- (void) createLabelX:(float) x Y:(float) y W:(float) w H:(float) h andText:(NSString *) s {
	//add info label
	UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
	lbl.text = s;
	lbl.textColor = [UIColor darkGrayColor];
	lbl.backgroundColor = [UIColor clearColor];
	[self addSubview:lbl];	
}
@end
